package io.github.uicdb.et.block;

import io.github.uicdb.et.ModBlockEntityType;
import io.github.uicdb.et.ModElements;
import io.github.uicdb.et.capability.IElementalHandler;
import io.github.uicdb.et.capability.SimpleElementalHandler;
import io.github.uicdb.et.recipe.ElementRecipe;
import io.github.uicdb.et.recipe.ElementRecipeInput;
import io.netty.buffer.Unpooled;
import lombok.Getter;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.world.ContainerHelper;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;

public class ElementMachineBlockEntity extends BaseContainerBlockEntity implements ICraft {
    NonNullList<ItemStack> list = NonNullList.withSize(10, ItemStack.EMPTY);//9输入。1输出
    @Getter
    final SimpleElementalHandler elementalHandler = new SimpleElementalHandler(16000) {
        @Override
        protected void onChange(ModElements elements, int before, int after) {
            ElementMachineBlockEntity.this.setChanged();
        }
    };

    @Getter
    private final ElementalContainerData containerData = new ElementalContainerData(elementalHandler);

    public ElementMachineBlockEntity(BlockPos pos, BlockState blockState) {
        super(ModBlockEntityType.ELEMENT_MACHINE.get(), pos, blockState);
    }

    @Override
    protected @NotNull Component getDefaultName() {
        return Component.translatable("container.element_machine");
    }

    @Override
    protected NonNullList<ItemStack> getItems() {
        return list;
    }

    @Override
    protected void setItems(NonNullList<ItemStack> nonNullList) {
        this.list = nonNullList;
        setChanged();
    }

    @Override
    protected @NotNull AbstractContainerMenu createMenu(int containerId, @NotNull Inventory inventory) {
        FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
        buf.writeBlockPos(getBlockPos());
        return new ElementMenu(containerId, inventory, this, containerData, buf);
    }

    @Override
    public int getContainerSize() {
        return this.list.size();
    }

    @Override
    protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
        super.saveAdditional(tag, registries);
        ContainerHelper.saveAllItems(tag, list, registries);
        tag.put("elements", elementalHandler.serializeNBT(registries));
    }

    @Override
    protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
        super.loadAdditional(tag, registries);
        ContainerHelper.loadAllItems(tag, list, registries);
        this.elementalHandler.deserializeNBT(registries, tag.getCompound("elements"));
    }

    public void tirggerCraft() {
        ElementRecipeInput input = new ElementRecipeInput(getElementalHandler(), list);
        if (!list.get(9).isEmpty()) return;
        level.getRecipeManager().getRecipeFor(ElementRecipe.TYPE, input, level).ifPresent(holder -> {
            var recipe = holder.value();
            recipe.getParts().forEach(input1 -> elementalHandler.drainElemental(input1.amount(), ModElements.byHolder(input1.element()), IElementalHandler.ElementalAction.EXECUTE));
            for (int i = 0; i < list.size(); i++) {
                if (i < 9) {
                    list.get(i).shrink(1);
                }
            }
            list.set(9, recipe.getResult().copy());
            setChanged();
        });
    }


}
